package com.fs.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.lang.UUID;

import com.fs.config.TravelConfig;
import com.fs.constant.CacheConstants;
import com.fs.constant.Constants;
import com.fs.util.RedisCache;
import com.fs.util.ResponseData;
import com.fs.util.ResponseDataUtil;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class CaptchaController {

   @Autowired
   @Qualifier("captchaProducer")
   private Producer captchaProducer;

    @Autowired
    @Qualifier("captchaProducerMath")
    private Producer captchaProducerMath;

    @Autowired
    private RedisCache redisCache;

    @RequestMapping("/auth/captcha")
    public ResponseData captchaImage() {
        //【2】.开启验证码,得到产生验证码的类型
        String captchaType = TravelConfig.getCaptchaType();
        System.out.println(captchaType);
        String imgStr="";
        String code="";
        //得到存储redis的唯一标识
        String uuid = UUID.randomUUID(false).toString(true);
        BufferedImage image = null;
        if("math".equals(captchaType)){
            //得到验证码的文本
            //1+1=?@2
            String text = captchaProducerMath.createText();
            String[] splitStr = text.split("@");
            imgStr = splitStr[0];
            code = splitStr[1];

            //2.根据验证码文本生成验证码图片
            image = captchaProducerMath.createImage(imgStr);

        }else if("char".equals(captchaType)){
            //得到验证码的文本
            //32ass
            String text = captchaProducer.createText();
            imgStr = code = text;
            //2.根据验证码文本生成验证码图片
            image = captchaProducer.createImage(imgStr);
        }
        //【3】把code答案保存的redis中
        String captchCacheKey = CacheConstants.CAPTCHA_CODE_KEY+uuid;
        redisCache.setCacheObject(captchCacheKey,code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        //【4】把图片转换为字节数组,并通过base64进行编码
        FastByteArrayOutputStream  sbo = new FastByteArrayOutputStream();
        try {
            ImageIO.write(image,"jpeg",sbo);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //Base64编码  java.util.Base64
        //【5】使用Base64对验证码字节数组进行编码, 封装到AjaxResult中
        //使用hutools
        String base64Str = "data:image/jpeg;base64,"+Base64.encode(sbo.toByteArray());
        //BASE64对密文进行传输加密时，可能出现\r\n
        //原因： RFC2045中有规定：即Base64一行不能超过76字符，超过则添加回车换行符。
        //解决方案: BASE64加密后，对\r\n进行去除
        base64Str= base64Str.replaceAll("\r|\n", "");

        //【6】响应AjaxResult的json给前端
        return ResponseDataUtil.buildOk(uuid,base64Str);
    }
}
